package com.fjh.springboot_01.service.impl;

import com.fjh.springboot_01.mapper.TutoringMapper;
import com.fjh.springboot_01.mapper.TutoringScheduleMapper;
import com.fjh.springboot_01.mapper.TutoringBookingMapper;
import com.fjh.springboot_01.pojo.tutor.Tutoring;
import com.fjh.springboot_01.pojo.tutor.TutoringBooking;
import com.fjh.springboot_01.pojo.tutor.TutoringSchedule;
import com.fjh.springboot_01.service.TutoringScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class TutoringScheduleServiceImpl implements TutoringScheduleService {
    @Autowired
    private TutoringScheduleMapper tutoringScheduleMapper;

    @Autowired
    private TutoringMapper tutoringMapper;
    
    @Autowired
    private TutoringBookingMapper tutoringBookingMapper;

    @Override
    public void createSchedule(TutoringSchedule schedule) {
        // 根据tutoring_id获取教师ID
        Integer teacherId = tutoringMapper.getTeacherIdByTutoringId(schedule.getTutoringId());
        if (teacherId == null) {
            throw new RuntimeException("未找到对应的教师信息");
        }
        
        // 验证该课程是否存在且状态为可用
        Tutoring tutoring = tutoringMapper.findById(schedule.getTutoringId());
        if (tutoring == null) {
            throw new RuntimeException("未找到对应的课程信息");
        }
        
        if (tutoring.getStatus() != 1) {
            throw new RuntimeException("该课程当前不可预约");
        }
        
        // 验证教师ID是否与课程中的教师ID一致
        if (!teacherId.equals(tutoring.getTeacherId())) {
            throw new RuntimeException("课程教师信息不匹配");
        }
        
        // 验证学生是否有权限预约该教师的课程
        // 检查学生是否有已确认的预约记录
        boolean hasBooking = tutoringBookingMapper.checkStudentTeacherBooking(
            schedule.getStudentId(), teacherId, 1) > 0;
            
        if (!hasBooking) {
            throw new RuntimeException("您没有与该教师的已确认预约，无法创建课程安排");
        }
        
        schedule.setTeacherId(teacherId);
        tutoringScheduleMapper.insert(schedule);
    }

    @Override
    public List<TutoringSchedule> getTeacherSchedules(Integer teacherId) {
        return tutoringScheduleMapper.selectByTeacherId(teacherId);
    }

    @Override
    public List<TutoringSchedule> getStudentSchedules(Integer studentId) {
        return tutoringScheduleMapper.selectByStudentId(studentId);
    }

    @Override
    public TutoringSchedule getScheduleById(Integer id) {
        return tutoringScheduleMapper.selectById(id);
    }

    @Override
    public void updateScheduleStatus(Integer id, Integer status) {
        tutoringScheduleMapper.updateStatus(id, status);
    }

    @Override
    public void submitFeedback(Integer id, String feedback, Integer rating) {
        tutoringScheduleMapper.updateFeedback(id, feedback, rating);
    }

    @Override
    public void teacherDeleteSchedule(Integer id) {
        // 标记为教师已删除
        tutoringScheduleMapper.markTeacherDeleted(id);
        // 尝试彻底删除（只有当学生也删除了才会真正删除）
        tutoringScheduleMapper.deleteById(id);
    }

    @Override
    public void studentDeleteSchedule(Integer id) {
        // 标记为学生已删除
        tutoringScheduleMapper.markStudentDeleted(id);
        // 尝试彻底删除（只有当教师也删除了才会真正删除）
        tutoringScheduleMapper.deleteById(id);
    }
} 